/*
 * Figures.cpp
 *
 *  Created on: 11-nov-2012
 *      Author: dna
 */

#include "Figures.h"

using namespace std;


Figures::Figures(int shape, GLfloat x, GLfloat y, GLfloat z, GLfloat size) {
	this->cx = x;
	this->cy = y;
	this->cz = z;
	this->size = size;

	this->faces = shape;

	this->xrot = 0.0;
	this->yrot = 0.0;
	this->zrot = 0.0;

	int n = 1; 				// vertex per face
	if (shape == F_CUBE)
		n = 4;

	this->vertex_per_faces = n;

	vdata = new F_VERTEX[shape*4];

	GLfloat a,b,c;

	for (int i=0; i<shape*n; i++){
		setSignCube(i,&a,&b,&c);
		fullVData(i,a*size/2,b*size/2,c*size/2,0.0,0.0);
		printf("%d %f %f %f \n",i,a*size/2,b*size/2,c*size/2);
	}

	cout << cx << " " << cy << " " << cz << " " << size << endl;
}

GLvoid Figures::fullVData(int n, GLfloat x, GLfloat y, GLfloat z, GLfloat u, GLfloat v){
	vdata[n].x = x;
	vdata[n].y = y;
	vdata[n].z = z;
	vdata[n].u = u;
	vdata[n].v = v;
}

GLvoid Figures::setSignCube(int i, GLfloat *a, GLfloat *b, GLfloat *c){
	switch(i){
	//			 X		  Y		   Z
	case 20:
	case 15:
	case 0: *a= 1.0; *b= 1.0; *c=-1.0; break;
	case 17:
	case 14:
	case 1: *a=-1.0; *b= 1.0; *c=-1.0; break;
	case 16:
	case 9:
	case 2: *a=-1.0; *b= 1.0; *c= 1.0; break;
	case 21:
	case 8:
	case 3: *a= 1.0; *b= 1.0; *c= 1.0; break;
	case 22:
	case 11:
	case 4: *a= 1.0; *b=-1.0; *c= 1.0; break;
	case 19:
	case 10:
	case 5: *a=-1.0; *b=-1.0; *c= 1.0; break;
	case 18:
	case 13:
	case 6: *a=-1.0; *b=-1.0; *c=-1.0; break;
	case 23:
	case 12:
	case 7: *a= 1.0; *b=-1.0; *c=-1.0; break;
	}
}

Figures::~Figures() {
	// TODO Auto-generated destructor stub
}

GLvoid Figures::drawFace(F_VERTEX v1, F_VERTEX v2, F_VERTEX v3, F_VERTEX v4){
	glBegin(GL_QUADS);
	glVertex3f(v1.x,v1.y,v1.z);
	glVertex3f(v2.x,v2.y,v2.z);
	glVertex3f(v3.x,v3.y,v3.z);
	glVertex3f(v4.x,v4.y,v4.z);
	glEnd();
}

GLvoid Figures::drawFace(F_VERTEX v1, F_VERTEX v2, F_VERTEX v3){
	glBegin(GL_TRIANGLES);
	glVertex3f(v1.x,v1.y,v1.z);
	glVertex3f(v2.x,v2.y,v2.z);
	glVertex3f(v3.x,v3.y,v3.z);
	glEnd();
}

GLvoid Figures::draw(){
	/*
	glBegin(GL_QUADS);                                      // Draw A Quad
	glColor3f(0.0f,1.0f,0.0f);          // Set The Color To Green
	glVertex3f( 1.0f, 1.0f,-1.0f);          // Top Right Of The Quad (Top)
	glVertex3f(-1.0f, 1.0f,-1.0f);          // Top Left Of The Quad (Top)
	glVertex3f(-1.0f, 1.0f, 1.0f);          // Bottom Left Of The Quad (Top)
	glVertex3f( 1.0f, 1.0f, 1.0f);          // Bottom Right Of The Quad (Top)
	glColor3f(1.0f,0.5f,0.0f);          // Set The Color To Orange
	glVertex3f( 1.0f,-1.0f, 1.0f);          // Top Right Of The Quad (Bottom)
	glVertex3f(-1.0f,-1.0f, 1.0f);          // Top Left Of The Quad (Bottom)
	glVertex3f(-1.0f,-1.0f,-1.0f);          // Bottom Left Of The Quad (Bottom)
	glVertex3f( 1.0f,-1.0f,-1.0f);          // Bottom Right Of The Quad (Bottom)
	glColor3f(1.0f,0.0f,0.0f);          // Set The Color To Red
	glVertex3f( 1.0f, 1.0f, 1.0f);          // Top Right Of The Quad (Front)
	glVertex3f(-1.0f, 1.0f, 1.0f);          // Top Left Of The Quad (Front)
	glVertex3f(-1.0f,-1.0f, 1.0f);          // Bottom Left Of The Quad (Front)
	glVertex3f( 1.0f,-1.0f, 1.0f);          // Bottom Right Of The Quad (Front)
	glColor3f(1.0f,1.0f,0.0f);          // Set The Color To Yellow
	glVertex3f( 1.0f,-1.0f,-1.0f);          // Bottom Left Of The Quad (Back)
	glVertex3f(-1.0f,-1.0f,-1.0f);          // Bottom Right Of The Quad (Back)
	glVertex3f(-1.0f, 1.0f,-1.0f);          // Top Right Of The Quad (Back)
	glVertex3f( 1.0f, 1.0f,-1.0f);          // Top Left Of The Quad (Back)
	glColor3f(0.0f,0.0f,1.0f);          // Set The Color To Blue
	glVertex3f(-1.0f, 1.0f, 1.0f);          // Top Right Of The Quad (Left)
	glVertex3f(-1.0f, 1.0f,-1.0f);          // Top Left Of The Quad (Left)
	glVertex3f(-1.0f,-1.0f,-1.0f);          // Bottom Left Of The Quad (Left)
	glVertex3f(-1.0f,-1.0f, 1.0f);          // Bottom Right Of The Quad (Left)
	glColor3f(1.0f,0.0f,1.0f);          // Set The Color To Violet
	glVertex3f( 1.0f, 1.0f,-1.0f);          // Top Right Of The Quad (Right)
	glVertex3f( 1.0f, 1.0f, 1.0f);          // Top Left Of The Quad (Right)
	glVertex3f( 1.0f,-1.0f, 1.0f);          // Bottom Left Of The Quad (Right)
	glVertex3f( 1.0f,-1.0f,-1.0f);          // Bottom Right Of The Quad (Right)
	glEnd();
	 */
	GLfloat versor[6][3] = {
			{ 0.0, 1.0, 0.0},
			{ 0.0,-1.0, 0.0},
			{ 0.0, 0.0,-1.0},
			{ 0.0, 0.0, 1.0},
			{ 1.0, 0.0, 0.0},
			{-1.0, 0.0, 0.0},
	};

	for (int i=0; i<this->faces*this->vertex_per_faces; i+=4){
		glNormal3fv(versor[i]);
		drawFace(vdata[i],vdata[i+1],vdata[i+2],vdata[i+3]);
	}
	glTranslatef(this->cx,this->cy,this->cz);
}

GLvoid Figures::increaseXrot(GLfloat value){
	xrot += value;
}

GLvoid Figures::increaseYrot(GLfloat value){
	yrot += value;
}

GLvoid Figures::increaseZrot(GLfloat value){
	zrot += value;
}

GLvoid Figures::increaseXrot(){
	xrot += 0.5;
}

GLvoid Figures::increaseYrot(){
	yrot += 0.5;
}

GLvoid Figures::increaseZrot(){
	zrot += 0.5;
}

GLvoid Figures::rotateX(){
	glTranslatef(cx,cy,cz);
	glRotatef(xrot,1.0,0.0,0.0);
	draw();
	glRotatef(-xrot,1.0,0.0,0.0);
	glTranslatef(-cx,-cy,-cz);
}

GLvoid Figures::rotateY(){
	glTranslatef(cx,cy,cz);
	glRotatef(yrot,0.0,1.0,0.0);
	draw();
	glRotatef(-yrot,0.0,1.0,0.0);
	glTranslatef(-cx,-cy,-cz);
}

GLvoid Figures::rotateZ(){
	glTranslatef(cx,cy,cz);
	glRotatef(zrot,0.0,0.0,1.0);
	draw();
	glRotatef(-zrot,0.0,0.0,1.0);
	glTranslatef(-cx,-cy,-cz);
}





















